package net.wanglu.www.zzz.rule.frag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import edu.stanford.nlp.ling.TaggedWord;

public class LonelyVerbFragments extends _FragmentsRule {

	Pattern lonelyVerb = Pattern.compile("^(\\bAnd/[A-Z]{2,4}\\b|\\bBut/[A-Z]{2,4}\\b|\\bFor/[A-Z]{2,4}\\b|\\bOr/[A-Z]{2,4}\\b|\\bNor/[A-Z]{2,4}\\b|\\bSo/[A-Z]{2,4}\\b|\\bYet/[A-Z]{2,4}\\b) \\w+/(\\bVB\\b|\\bVBD\\b|\\bVBN\\b|\\bVBP\\b|\\bVBZ\\b)");
	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb.append("可以把孤单动词残缺句放在一个独立主句后面, 使之成为主句的一部分, 表达出完成的意思. 例如:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("误: But knew that all of his effort would prove useless in the long run.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("正: With a heavy sigh, Darryl began counting the words of his essay but knew that all of his effort would prove useless in the long run.");
		sb.append("</p>");
		return sb.toString();
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		HashMap<?,?> preprocessData = (HashMap<?, ?>) objects[0];
		ArrayList<?> twl = (ArrayList<?>) preprocessData.get("twl");
		String sentence = (String) preprocessData.get("sentence");
		
		sb.clear();
		for( Iterator<?> itr = twl.iterator(); itr.hasNext();) {
			TaggedWord tw = (TaggedWord) itr.next();
			sb.append(tw).append(" ");
		}
		
		if ( lonelyVerb.matcher(sb.toString()).find()) {
			BadEgg egg = new BadEgg();
			egg.setViolatedRule(this);
			egg.whereIsWrong = sentence;
			return egg;
		} else {
			return null;
		}
	}
	
	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb.append("孤单动词残缺句往往是由于作者遗漏了句子的主语造成的. 这种残缺句经常以and, but, for, or, nor, so, yet这些并列连词开头. 例如:");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("And dashed through the downpour as raindrops softened the hairspray shell holding her elaborate coif in place.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("But knew that all of his effort would prove useless in the long run.");
		sb.append("</p>");
		sb.append("<p>");
		sb.append("Took the thick book and, with a heavy sigh, loaded it on top of her research pile.");
		sb.append("</p>");
		return sb.toString();
	}

}
